home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Sample Code / QuickDraw / OffSample / OffSample.p < prev    next >
Encoding:
Text File  |  1994-11-18  |  44.3 KB  |  1,532 lines  |  [TEXT/MPS ]

  1. {------------------------------------------------------------------------------
  2. #
  3. #    Apple Macintosh Developer Technical Support
  4. #
  5. #    Offscreen Buffer Sample Application
  6. #
  7. #    OffSample
  8. #
  9. #    OffSample.p        -    Pascal Source
  10. #
  11. #    Copyright © 1989 Apple Computer, Inc.
  12. #    All rights reserved.
  13. #
  14. #    Versions:    
  15. #                1.00                04/89
  16. #                1.01                06/92
  17. #
  18. #    Components:
  19. #                OffSample.p            April 1, 1989
  20. #                OffSample.r            April 1, 1989
  21. #                OffSample.h            April 1, 1989
  22. #                OffSample.rsrc        April 1, 1989
  23. #                POffSample.make        April 1, 1989
  24. #
  25. #    Requirements:
  26. #                Offscreen.p            April 1, 1989
  27. #                Offscreen.inc1.p    April 1, 1989
  28. #                UFailure.p            November 1, 1988
  29. #                UFailure.inc1.p        November 1, 1988
  30. #                UFailure.a            November 1, 1988
  31. #
  32. #    OffSample demonstrates the usage of the Offscreen
  33. #    unit. It shows how to use offscreen pixmaps and
  34. #    bitmaps to produce flicker-free updating with a
  35. #    minimum of re-structuring of code. OffSample attempts
  36. #    to reduce the amount of 'knowledge' that it has of
  37. #    the offscreen structure so as to minimize its
  38. #    dependence on that unit.
  39. #
  40. #    OffSample emphasizes using the Offscreen unit; it
  41. #    is not intended to be viewed as a complete application
  42. #    from which to base some larger effort. Instead, its
  43. #    method of using offscreen bitmaps and pixmaps should
  44. #    be studied and adapted to other applications that
  45. #    desire features such as flicker-free updating.
  46. #
  47. ------------------------------------------------------------------------------}
  48.  
  49.  
  50. PROGRAM OffSample;
  51.  
  52. USES
  53.     Types, QuickDraw, Palettes, Events, Controls, Windows, TextEdit, Dialogs,
  54.     Fonts, Lists, Menus, Resources, Scrap, ToolUtils, 
  55.     OSUtils, Files, Devices, DeskBus, DiskInit, Disks, Errors, Memory, Retrace, SegLoad, Serial,
  56.     ShutDown, Slots, Sound, Start, Timer,
  57.     Packages, ColorPicker, UFailure, Offscreen, Traps;
  58.  
  59. CONST
  60.  
  61.     kSysEnvironsVersion        = 1;
  62.  
  63.     kOSEvent                = app4Evt;        {event used by MultiFinder}
  64.     kSuspendResumeMessage    = 1;            {high byte of suspend/resume event message}
  65.     kResumeMask                = 1;            {bit of msg field for resume vs. suspend}
  66.  
  67.     kMinHeap                = 66 * 1024;
  68.     
  69.     kMinSpace                = 49 * 1024;
  70.     
  71.     kExtremeNeg                = -32768;
  72.     kExtremePos                = 32767 - 1;    {required for old region bug}
  73.     
  74.     sErrStrings                = 128;            {error string STR#}
  75.     eStandardErr            = 1;
  76.     eWrongMachine            = 2;
  77.     eSmallSize                = 3;
  78.     eNoMemory                = 4;
  79.     
  80.     kNoBackBuff                = 128;
  81.     kNoEditBuff                = 129;
  82.     kTitle                    = 130;
  83.     kColorPrompt            = 131;
  84.     kNoWantBack                = 132;
  85.     kNoWantEdit                = 133;
  86.     
  87.     kCMoof                    = 128;
  88.     kGigantor                = 128;
  89.     k1bitGigantor            = 129;
  90.     
  91.     rMenuBar                = 128;            {application's menu bar}
  92.     rAboutAlert                = 128;            {about alert}
  93.     rUserAlert                = 129;            {error user alert}
  94.     rWindow                    = 128;            {application's window}
  95.  
  96.     mApple                    = 128;            {Apple menu}
  97.     iAbout                    = 1;
  98.  
  99.     mFile                    = 129;            {File menu}
  100.     iNew                    = 1;
  101.     iClose                    = 4;
  102.     iQuit                    = 12;
  103.  
  104.     mEdit                    = 130;            {Edit menu}
  105.     iUndo                    = 1;
  106.     iCut                    = 3;
  107.     iCopy                    = 4;
  108.     iPaste                    = 5;
  109.     iClear                    = 6;
  110.     
  111.     mShape                    = 131;            {Shape menu}
  112.     
  113.     mSpecial                = 132;            {Special menu}
  114.     iUseBack                = 1;
  115.     iUseEdit                = 2;
  116.     iPickColor                = 4;
  117.  
  118.     kDITop                    = $0050;
  119.     kDILeft                    = $0070;
  120.     
  121.     kNotDrawn                = -1;
  122.     kLastOne                = -2;
  123.     
  124.     kCursorDepth            = 2;
  125.     kMemoryPolite            = TRUE;
  126.     
  127.     kFramePenH                = 2;
  128.     kFramePenV                = 2;
  129.     
  130.     
  131. TYPE
  132.  
  133.     Shapes            = (kOval, kRegion, kRRect, kPoly, kRect, kICON, kPICT);
  134.  
  135.     ShapeRecord        = RECORD
  136.         next            : Shapes;        {when is it drawn?}
  137.         extent            : Rect;            {where is it?}
  138.     END;
  139.                     
  140.     ShapeArray        = ARRAY [Shapes] OF ShapeRecord;
  141.     
  142.     {An OffscreenRecord contains the WindowRecord for one of our sample windows,
  143.      as well as an offscreen handle for the background and an offscreen handle
  144.      for the background plus the shape being created. It also has an array of
  145.      shapes for this window, a pointer to the first shape, a pointer to the
  146.      shape being edited, and a record of the last state of the buffers. For a
  147.      similar example of extending a toolbox data structure, see how the Window
  148.      Manager and Dialog Manager add fields to the GrafPort and WindowRecord,
  149.      respectively.}
  150.      
  151.     OffscreenRecord    = RECORD
  152.         fWindow        : WindowRecord;        {window data structure for toolbox use}
  153.         fBackHandle    : Handle;            {offscreen pixmap that holds background}
  154.         fEditHandle    : Handle;            {pixmap for background and shape being created}
  155.         fShapes        : ShapeArray;        {the shapes for this window}
  156.         fFirst        : Shapes;            {who is first?}
  157.         fEdit        : Shapes;            {who is being edited?}
  158.         fHasBack    : BOOLEAN;            {did it have a background buffer last time?}
  159.         fHasEdit    : BOOLEAN;            {did it have a edit buffer last time?}
  160.     END;
  161.     OffscreenPeek    = ^OffscreenRecord;
  162.  
  163.  
  164. VAR
  165.     {The "g" prefix is used to emphasize that a variable is global.}
  166.  
  167.     gMac                : SysEnvRec;    {set up by Initialize}
  168.     gHasWaitNextEvent    : BOOLEAN;        {set up by Initialize}
  169.     gInBackground        : BOOLEAN;        {maintained by Initialize and DoEvent}
  170.     
  171.     gShape                : Shapes;        {current shape}
  172.     gUseBack            : BOOLEAN;        {create background offscreen flag}
  173.     gUseEdit            : BOOLEAN;        {create edit offscreen flag}
  174.     gCursor                : CCrsrHandle;    {there can be ONLY one}
  175.     gOughHandle            : Handle;        {offscreen handle for color cursor}
  176.     gPICT                : PicHandle;    {Gigantor}
  177.     gcicn                : CIconHandle;    {Moof!™}
  178.     g1BitHandle            : Handle;        {for the color cursor mask}
  179.  
  180.  
  181. {$S Initialize}
  182. FUNCTION TrapAvailable(tNumber: INTEGER; tType: TrapType): BOOLEAN;
  183.  
  184. {Check to see if a given trap is implemented. This is only used by the
  185.  Initialize routine in this program, so we put it in the Initialize segment.
  186.  The recommended approach to see if a trap is implemented is to see if
  187.  the address of the trap routine is the same as the address of the
  188.  Unimplemented trap. Needs to be called after call to SysEnvirons so that
  189.  it can check if a ToolTrap is out of range of a pre-MacII ROM.}
  190.  
  191. BEGIN
  192.     IF (tType = ToolTrap) &
  193.         (gMac.machineType > envMachUnknown) &
  194.         (gMac.machineType < envMacII) THEN BEGIN        {it's a 512KE, Plus, or SE}
  195.         tNumber := BAND(tNumber, $03FF);
  196.         IF tNumber > $01FF THEN                            {which means the tool traps}
  197.             tNumber := _Unimplemented;                    {only go to $01FF}
  198.     END;
  199.     TrapAvailable := NGetTrapAddress(tNumber, tType) <>
  200.                         NGetTrapAddress(_Unimplemented, ToolTrap);
  201. END; {TrapAvailable}
  202.  
  203.  
  204. {$S Main}
  205. PROCEDURE GetGlobalRect (window: WindowPtr; VAR globalRect: Rect);
  206.  
  207. {Return the portRect of window in global coordinates.}
  208.  
  209. VAR
  210.     savePort    : GrafPtr;
  211.     
  212. BEGIN
  213.     GetPort(savePort);
  214.     SetPort(window);                    {so that the correct }
  215.     globalRect := window^.portRect;        { coordinate system is used}
  216.     WITH globalRect DO BEGIN
  217.         LocalToGlobal(topLeft);
  218.         LocalToGlobal(botRight);
  219.     END;
  220.     SetPort(savePort);
  221. END; {GetGlobalRect}
  222.  
  223.  
  224. {$S Main}
  225. FUNCTION IsDAWindow (window: WindowPtr): BOOLEAN;
  226.  
  227. {Check if a window belongs to a desk accessory.}
  228.  
  229. BEGIN
  230.     IF window = NIL THEN
  231.         IsDAWindow := FALSE
  232.     ELSE    {DA windows have negative windowKinds}
  233.         IsDAWindow := WindowPeek(window)^.windowKind < 0;
  234. END; {IsDAWindow}
  235.  
  236.  
  237. {$S Main}
  238. FUNCTION IsAppWindow (window: WindowPtr): BOOLEAN;
  239.  
  240. {Check to see if a window belongs to the application. If the window pointer
  241.  passed was NIL, then it could not be an application window. WindowKinds
  242.  that are negative belong to the system and windowKinds less than userKind
  243.  are reserved by Apple except for windowKinds equal to dialogKind, which
  244.  mean it is a dialog.}
  245.  
  246. BEGIN
  247.     IF window = NIL THEN
  248.         IsAppWindow := FALSE
  249.     ELSE    {application windows have windowKinds >= userKind (8)}
  250.         WITH WindowPeek(window)^ DO
  251.             IsAppWindow := (windowKind = userKind);
  252. END; {IsAppWindow}
  253.  
  254.  
  255. {$S Main}
  256. PROCEDURE FailNILMsg(p: UNIV Ptr; message: INTEGER);
  257.  
  258. {Check for NIL p and fail if so.}
  259.  
  260. BEGIN
  261.     IF p = NIL THEN
  262.         Failure(memFullErr, message);
  263. END; {FailNILMsg}
  264.  
  265.  
  266. {$S Main}
  267. PROCEDURE AlertUser(error: INTEGER; message: LongInt);
  268.  
  269. {Display an alert to inform the user of an error. Message acts as an 
  270.  index into a STR# resource of error messages. If no message is given,
  271.  i.e. = 0, then use a standard message. If error is not noErr then
  272.  display it as well.}
  273.  
  274. VAR
  275.     msg1, msg2    : Str255;
  276.     itemHit        : INTEGER;
  277. BEGIN
  278.     IF message = 0 THEN message := eStandardErr;
  279.     GetIndString(msg1, sErrStrings, message);
  280.     IF error = noErr THEN
  281.         msg2 := ''
  282.     ELSE
  283.         NumToString(error, msg2);
  284.     ParamText(msg1, msg2, '', '');
  285.     itemHit := Alert(rUserAlert, NIL);
  286. END; {AlertUser}
  287.  
  288.  
  289. {$S Main}
  290. FUNCTION DoCloseWindow(window: WindowPtr) : BOOLEAN;
  291.  
  292. {Close a window.}
  293.  
  294. {At this point, if there was a document associated with a
  295.  window, you could do any document saving processing if it is 'dirty'.
  296.  DoCloseWindow would return TRUE if the window actually closes, i.e.,
  297.  the user does not cancel from a save dialog. This result is handy when
  298.  the user quits an application, but then cancels a save of a document
  299.  associated with a window. We also added code to close the application
  300.  window since otherwise, the termination routines would never stop looping,
  301.  waiting for FrontWindow to return NIL.}
  302.  
  303. VAR
  304.     pal    : PaletteHandle;
  305.  
  306. BEGIN
  307.     DoCloseWindow := TRUE;
  308.     IF IsDAWindow(window) THEN
  309.         CloseDeskAcc(WindowPeek(window)^.windowKind);
  310.     IF IsAppWindow(window) THEN BEGIN
  311.         WITH OffscreenPeek(window)^ DO BEGIN
  312.             DisposeOffscreen(fBackHandle);
  313.             DisposeOffscreen(fEditHandle);
  314.         END;
  315.         IF gMac.hasColorQD THEN BEGIN
  316.             pal := GetPalette(window);            {We must handle this ourselves,}
  317.             DisposePalette(pal);                {since we may have done a GetPalette.}
  318.         END;
  319.         CloseWindow(window);                    {Since we provided our own storage.}
  320.         DisposePtr(Ptr(window));
  321.     END;
  322. END; {DoCloseWindow}
  323.  
  324.  
  325. {$S Main}
  326. PROCEDURE EfficientConcat2 (VAR string1, string2: Str255);
  327.  
  328. {Do a more efficient concat than CONCAT since we know
  329.  there are only two strings.}
  330.  
  331. VAR
  332.     len1, len2    : INTEGER;
  333.     
  334. BEGIN
  335.     len1 := LENGTH(string1);
  336.     IF len1 < 255 THEN BEGIN
  337.         len2 := LENGTH(string2);
  338.         IF len1 + len2 > 255 THEN
  339.             len2 := 255 - len1;
  340.         BlockMove(@string2[1], @string1[1 + len1], len2);
  341.         string1[0] := CHR(len1 + len2);
  342.     END;
  343. END; {EfficientConcat2}
  344.  
  345.  
  346. {$S Main}
  347. PROCEDURE AppendTitle (VAR title: Str255; id: INTEGER);
  348.  
  349. {Append the specified string resource data to the provided
  350.  string.}
  351.  
  352.  
  353. VAR
  354.     aString    : StringHandle;
  355.     
  356. BEGIN
  357.     aString := GetString(id);
  358.     IF aString <> NIL THEN BEGIN
  359.         HLock(Handle(aString));                {in case EfficientConcat2 is}
  360.         EfficientConcat2(title, aString^^);
  361.         HUnlock(Handle(aString));            {in a different segment}
  362.     END;
  363. END; {AppendTitle}
  364.  
  365.  
  366. {$S Main}
  367. PROCEDURE CheckTitle (window: WindowPtr; doCheck: BOOLEAN);
  368.  
  369. {Compare the prior state of the offscreen handles for
  370.  window and change its title to reflect the new state.}
  371.  
  372. VAR
  373.     aString                : StringHandle;
  374.     title                : Str255;
  375.     hasBack, hasEdit    : BOOLEAN;
  376.  
  377. BEGIN
  378.     IF IsAppWindow(window) THEN
  379.         WITH OffscreenPeek(window)^ DO BEGIN
  380.             hasBack := (GetMap(fBackHandle) <> NIL);
  381.             hasEdit := (GetMap(fEditHandle) <> NIL);
  382.             IF (NOT doCheck) |                    {set title regardless}
  383.             (fHasBack <> hasBack) |                {or if change}
  384.             (fHasEdit <> hasEdit) THEN BEGIN    {in buffers}
  385.                 fHasBack := hasBack;
  386.                 fHasEdit := hasEdit;
  387.                 title := '';
  388.                 aString := GetString(kTitle);
  389.                 IF aString <> NIL THEN
  390.                     title := aString^^;
  391.                     
  392.                 {If an offscreen handle is NIL, it means
  393.                  that the creation of that offscreen handle
  394.                  was disabled by the user. Once that is
  395.                  done, the buffer will never be created.}
  396.                 
  397.                 IF fBackHandle = NIL THEN
  398.                     AppendTitle(title, kNoWantBack)
  399.                 ELSE IF NOT hasBack THEN
  400.                     AppendTitle(title, kNoBackBuff);
  401.                 IF fEditHandle = NIL THEN
  402.                     AppendTitle(title, kNoWantEdit)
  403.                 ELSE IF NOT hasEdit THEN
  404.                     AppendTitle(title, kNoEditBuff);
  405.                 SetWTitle(window, title);
  406.             END;
  407.         END;
  408. END; {CheckTitle}
  409.  
  410.  
  411. {$S Main}
  412. PROCEDURE DrawShape (shape: Shapes; VAR extent: Rect);
  413.  
  414. {Draw the shape specified in the extent. Extent is a VAR
  415.  parameter because the region and polygon are generated
  416.  from the extent rect and the calculations might result
  417.  in a final shape larger than the original extent.}
  418.  
  419.     PROCEDURE DoRegion;
  420.     
  421.     {Generate a region based on the extent.}
  422.     
  423.     VAR
  424.         r        : Rect;
  425.         rHandle    : RgnHandle;
  426.         pHandle    : PolyHandle;
  427.         
  428.     BEGIN
  429.         r := extent;
  430.         rHandle := NewRgn;
  431.         OpenRgn;
  432.         
  433.         FrameRect(extent);
  434.         WITH r DO BEGIN
  435.             top := top + ((bottom - top) DIV 3);
  436.             bottom := top + ((bottom - top) DIV 2);
  437.         END;
  438.         FrameOval(r);
  439.         pHandle := OpenPoly;
  440.         WITH extent DO BEGIN
  441.             MoveTo(left, top);
  442.             LineTo(right, bottom);
  443.             LineTo(left + (right - left) DIV 2, bottom - (bottom - top) DIV 3);
  444.             LineTo(left, top);
  445.         END;
  446.         ClosePoly;
  447.         FramePoly(pHandle);
  448.         KillPoly(pHandle);
  449.         
  450.         CloseRgn(rHandle);
  451.         extent := rHandle^^.rgnBBox;        {in case bigger than original rect}
  452.         IF gMac.hasColorQD THEN
  453.             PaintRgn(rHandle)
  454.         ELSE
  455.             FillRgn(rHandle, qd.black);
  456.         ForeColor(blackColor);
  457.         FrameRgn(rHandle);
  458.         DisposeRgn(rHandle);
  459.     END; {DoRegion}
  460.     
  461.     PROCEDURE DoPoly;
  462.     
  463.     {Generate a polygon based on the extent.}
  464.     
  465.     VAR
  466.         pHandle    : PolyHandle;
  467.         
  468.     BEGIN
  469.         pHandle := OpenPoly;
  470.         WITH extent DO BEGIN
  471.             MoveTo(left + (right - left) DIV 2, top);
  472.             LineTo(right, bottom);
  473.             LineTo(left, top + (bottom - top) DIV 3);
  474.             LineTo(right, top + (bottom - top) DIV 3);
  475.             LineTo(left, bottom);
  476.             LineTo(left + (right - left) DIV 2, top);
  477.         END;
  478.         ClosePoly;
  479.         extent := pHandle^^.polyBBox;        {in case bigger than original rect}
  480.         IF gMac.hasColorQD THEN
  481.             PaintPoly(pHandle)
  482.         ELSE
  483.             FillPoly(pHandle, qd.ltGray);
  484.         ForeColor(blackColor);
  485.         FramePoly(pHandle);
  486.         KillPoly(pHandle);
  487.     END; {DoPoly}
  488.     
  489. BEGIN
  490.     PenNormal;
  491.     PenSize(kFramePenH, kFramePenV);
  492.     CASE shape OF
  493.         kOval: BEGIN
  494.             IF gMac.hasColorQD THEN
  495.                 PaintOval(extent)
  496.             ELSE
  497.                 FillOval(extent, qd.white);
  498.             ForeColor(blackColor);
  499.             FrameOval(extent);
  500.         END;
  501.         kRegion:
  502.             DoRegion;
  503.         kRRect: BEGIN
  504.             IF gMac.hasColorQD THEN
  505.                 PaintRoundRect(extent, 16, 16)
  506.             ELSE
  507.                 FillRoundRect(extent, 16, 16, qd.gray);
  508.             ForeColor(blackColor);
  509.             FrameRoundRect(extent, 16, 16);
  510.         END;
  511.         kPoly:
  512.             DoPoly;
  513.         kRect: BEGIN
  514.             IF gMac.hasColorQD THEN
  515.                 PaintRect(extent)
  516.             ELSE
  517.                 FillRect(extent, qd.dkGray);
  518.             ForeColor(blackColor);
  519.             FrameRect(extent);
  520.         END;
  521.         kICON:
  522.             IF gMac.hasColorQD THEN
  523.                 PlotCIcon(extent, gcicn)
  524.             ELSE BEGIN 
  525.                 HLock(Handle(gcicn));
  526.                 WITH gcicn^^ DO BEGIN
  527.                 {We cannot call PlotCIcon when Color QD is not
  528.                  present, but we can still use the color icon
  529.                  data.}
  530.                     iconMask.baseAddr := @iconMaskData;
  531.                     iconBMap.baseAddr := Ptr(ORD(@iconMaskData) + 128);
  532.                     CopyMask(iconBMap, iconMask, qd.thePort^.portBits,
  533.                                 iconBMap.bounds, iconMask.bounds, extent);
  534.                 END;
  535.                 HUnlock(Handle(gcicn));
  536.             END;
  537.         kPICT:
  538.             DrawPicture(gPICT, extent);
  539.     END;
  540. END; {DrawShape}
  541.  
  542.  
  543. {$S Main}
  544. FUNCTION GimmeBlackAndWhite(rgb: RGBColor; VAR position: LONGINT): BOOLEAN;
  545.  
  546. {This is a search proc that returns white only if the color is really white;
  547.  otherwise it returns black. It is used to generate the mask for the color
  548.  cursor. It boldly assumes that it is being called for a 1 bit deep map.}
  549.  
  550. BEGIN
  551.     WITH rgb DO
  552.         IF (red = $FFFF) & (green = $FFFF) & (blue = $FFFF) THEN
  553.             position := 0                    {return white if it’s white}
  554.         ELSE
  555.             position := 1;                    {else return black for all other colors}
  556.     GimmeBlackAndWhite := TRUE;
  557. END; {GimmeBlackAndWhite}
  558.  
  559.  
  560. {$S Main}
  561. PROCEDURE SetObjCursor (window: WindowPtr);
  562.  
  563. {Build the color cursor. Note that this routine is only called
  564.  in a Color QD environment, so it doesn't have to make the
  565.  check. Note also that the cursors could have all been 'pre-
  566.  built', thus making things more efficient, but this example
  567.  shows that dynamic cursors can be implemented via pixmaps.}
  568.  
  569. VAR
  570.     colors            : CTabHandle;
  571.     pal                : PaletteHandle;
  572.     rgb                : RGBColor;
  573.     bounder, extent    : Rect;
  574.     buffNotNeeded    : BOOLEAN;
  575.     naughtyBits        : BitMap;
  576.     oneBitPMap        : BitMapPtr;
  577.  
  578. BEGIN
  579.     SetRect(bounder, 0, 0, 16, 16);
  580.     pal := GetPalette(window);
  581.     GetEntryColor(pal, ORD(gShape) + 2, rgb);    {get the color used for the shape}
  582.     
  583.     DisposeOffscreen(gOughHandle);                {get rid of old color table}
  584.     colors := CTabHandle(NewHandleClear(SIZEOF(ColorTable)));
  585.     FailNILMsg(colors, eNoMemory);
  586.     colors^^.ctTable[0].rgb := rgb;                {stuff in the color we want}
  587.     
  588.     FailOSErr(NewOffscreen(bounder, kCursorDepth, colors,
  589.                     NOT kMemoryPolite, buffNotNeeded,
  590.                     gOughHandle));
  591.     DisposeHandle(Handle(colors));
  592.     
  593.     HLock(Handle(gCursor));
  594.     WITH gCursor^^ DO BEGIN
  595.         crsrMap := PixMapHandle(RecoverHandle(Ptr(GetMap(gOughHandle))));
  596.         crsrData := GetBitsHandle(gOughHandle);
  597.         IF crsrData = NIL THEN BEGIN            {no handle to bits available-punt}
  598.             SetCursor(qd.arrow);
  599.             Exit(SetObjCursor);
  600.         END;
  601.         BeginOffscreenDrawing(gOughHandle, NIL);
  602.         IF NOT (gShape IN [kICON, kPICT]) THEN BEGIN
  603.             SetPt(crsrHotSpot, 0, 0);
  604.             RGBForeColor(rgb);
  605.             extent := bounder;
  606.             InsetRect(extent, 3, 1);            {squeeze it a bit}
  607.             DrawShape(gShape, extent);            {draw the cursor shape}
  608.             PenNormal;
  609.             ForeColor(blackColor);                {draw hot spot}
  610.             MoveTo(0, 0);
  611.             LineTo(0, 1);
  612.         END ELSE BEGIN                            {use a plain cursor for icon/pict}
  613.             SetPt(crsrHotSpot, 2, 2);
  614.             PenNormal;
  615.             ForeColor(blackColor);
  616.             MoveTo(0, 0);
  617.             LineTo(4, 4);
  618.             MoveTo(4, 0);
  619.             LineTo(0, 4);
  620.         END;
  621.         EndOffscreenDrawing(gOughHandle);
  622.         WITH naughtyBits DO BEGIN                {build 1-bit image and mask}
  623.             bounds := bounder;
  624.             baseAddr := @crsr1Data;
  625.             rowBytes := 2;
  626.             CopyBits(BitMapPtr(crsrMap^)^, naughtyBits,
  627.                         bounder, bounder, srcCopy, NIL);
  628.                         
  629.             oneBitPMap :=  GetMap(g1BitHandle);
  630.             oneBitPMap^.baseAddr := @crsrMask;
  631.             AddSearch(@GimmeBlackAndWhite);
  632.             CopyBits(BitMapPtr(crsrMap^)^, oneBitPMap^,
  633.                         bounder, bounder, srcCopy, NIL);
  634.             DelSearch(@GimmeBlackAndWhite);
  635.         END;
  636.         crsrXValid := 0;
  637.         crsrID := GetCTSeed;
  638.     END;
  639.     HUnlock(Handle(gCursor));
  640. END; {SetObjCursor}
  641.  
  642.  
  643. {$S Main}
  644. FUNCTION GetInvalExtent (window: WindowPtr; shape: Shapes) : Rect;
  645.  
  646. {Return the shape's extent, adjusted for the pensize of the frame.}
  647.  
  648. VAR
  649.     r    : Rect;
  650.     
  651. BEGIN
  652.     r := OffscreenPeek(window)^.fShapes[shape].extent;
  653.     WITH r DO BEGIN
  654.         right := right + kFramePenH;
  655.         bottom := bottom + kFramePenV;
  656.     END;
  657.     GetInvalExtent := r;
  658. END; {GetInvalExtent}
  659.  
  660.  
  661. {$S Main}
  662. PROCEDURE ChangeColor (window: WindowPtr);
  663.  
  664. {Display the Color ColorPicker dialog. Note that this is
  665.  only called in Color QD environments.}
  666.  
  667. VAR
  668.     pal                    : PaletteHandle;
  669.     inColor, outColor    : RGBColor;
  670.     where                : Point;
  671.     r                    : Rect;
  672.     aString                : StringHandle;
  673.     prompt                : Str255;
  674.  
  675. BEGIN
  676.     pal := GetPalette(window);
  677.     WITH OffscreenPeek(window)^ DO BEGIN
  678.         GetEntryColor(pal, ORD(gShape) + 2, inColor);
  679.         SetPt(where, kDILeft, kDITop);
  680.         aString := GetString(kColorPrompt);
  681.         IF aString <> NIL THEN
  682.             prompt := aString^^
  683.         ELSE
  684.             prompt := '';
  685.         IF GetColor(where, prompt, inColor, outColor) THEN BEGIN
  686.             SetEntryColor(pal, ORD(gShape) + 2, outColor);
  687.             ActivatePalette(window);
  688.             SetObjCursor(window);
  689.             IF NOT (fShapes[gShape].next = Shapes(kNotDrawn)) THEN BEGIN
  690.                 r := GetInvalExtent(window, gShape);
  691.                 SetPort(window);
  692.                 InvalRect(r);
  693.             END;
  694.         END;
  695.     END;
  696. END; {ChangeColor}
  697.  
  698.  
  699. {$S Main}
  700. PROCEDURE DoNewWindow;
  701.  
  702. {We will allocate our own window storage instead of letting the Window
  703.  Manager do it for two reasons. One, GetNewWindow locks the 'WIND' resource
  704.  handle before calling NewWindow and this can lead to heap fragmentation
  705.  in low memory situations. Two, it takes just as much time for NewWindow
  706.  to get the memory as it does for us to get it. Three, there are THREE
  707.  reasons we will allocate our own window storage instead of letting the
  708.  Window Manager do it. One, GetNewWindow locks etc. etc. Two, it takes
  709.  just as much time, etc. etc. And three, this way we can allocate larger records
  710.  where the extra space can be used to connect other, related data structures.
  711.  Four, there is no fourth reason.}
  712.  
  713. VAR
  714.     p                : Ptr;
  715.     window            : WindowPtr;
  716.     noBuffsPlease    : BOOLEAN;
  717.     title            : Str255;
  718.     shape            : Shapes;
  719.     emptyRect        : Rect;
  720.  
  721. BEGIN
  722.     p := NewPtr(SIZEOF(OffscreenRecord));
  723.     FailNILMsg(p, eNoMemory);
  724.     window := NIL;
  725.     IF gMac.hasColorQD THEN
  726.         window := GetNewCWindow(rWindow, p, WindowPtr(-1))
  727.     ELSE
  728.         window := GetNewWindow(rWindow, p, WindowPtr(-1));
  729.     FailNILMsg(window, eNoMemory);
  730.     
  731.     WITH OffscreenPeek(window)^ DO BEGIN
  732.         fBackHandle := NIL;
  733.         fEditHandle := NIL;
  734.         fHasBack := FALSE;
  735.         fHasEdit := FALSE;
  736.         IF gUseBack THEN
  737.             IF NewOffscreenForWindow(window, noBuffsPlease, fBackHandle) = noErr THEN;
  738.         IF gUseEdit THEN
  739.             IF NewOffscreenForWindow(window, noBuffsPlease, fEditHandle) = noErr THEN;
  740.         SetRect(emptyRect, 0, 0, 0, 0);
  741.         FOR shape := kOval TO kPICT DO BEGIN
  742.             fShapes[shape].next := Shapes(kNotDrawn);
  743.             fShapes[shape].extent := emptyRect;
  744.         END;
  745.         fFirst := Shapes(kNotDrawn);
  746.         fEdit := Shapes(kNotDrawn);
  747.     END;
  748.     
  749.     CheckTitle(window, FALSE);
  750.     IF gMac.hasColorQD THEN
  751.         SetObjCursor(window);
  752. END; {DoNewWindow}
  753.  
  754.  
  755. {$S Initialize}
  756. PROCEDURE Initialize;
  757.  
  758. {Set up the whole world, including global variables, Toolbox managers,
  759.  and menus. We also create one application window at this time.
  760.  Since window storage is non-relocateable, how and when to allocate space
  761.  for windows is very important so that heap fragmentation does not occur.
  762.  Window storage can differ widely amongst applications depending on how many
  763.  windows are created and disposed. If a failure occurs here, we will consider
  764.  that the application is in such bad shape that we should just exit. Your error
  765.  handling may differ, but the checks should still be made.}
  766.  
  767. TYPE
  768.     crsColors = ARRAY[0..2] OF ColorSpec;
  769.  
  770. VAR
  771.     menuBar            : Handle;
  772.     ignoreError        : OSErr;
  773.     total, contig    : LongInt;
  774.     ignoreResult    : BOOLEAN;
  775.     event            : EventRecord;
  776.     count            : INTEGER;
  777.     fi                : FailInfo;
  778.     colors            : CTabHandle;
  779.     bounder            : Rect;
  780.     buffNotNeeded    : BOOLEAN;
  781.  
  782.     PROCEDURE HandleErr(error: INTEGER; message: LongInt);
  783.     BEGIN
  784.         IF error > 0 THEN
  785.             AlertUser(0, error)
  786.         ELSE
  787.             AlertUser(error, message);
  788.         ExitToShell;
  789.     END; {HandleErr}
  790.  
  791. BEGIN
  792.     gInBackground := FALSE;
  793.  
  794.     InitGraf(@qd.thePort);
  795.     InitFonts;
  796.     InitWindows;
  797.     InitMenus;
  798.     TEInit;
  799.     InitDialogs(NIL);
  800.     InitCursor;
  801.     
  802.     InitOffscreen;
  803.  
  804.     {Call OpenDriver('.MPP', refnum) at this point to initialize AppleTalk,
  805.      if you are using it.}
  806.      
  807.     {NOTE -- It is no longer necessary, and actually unhealthy, to check
  808.      PortBUse and SPConfig before opening AppleTalk. The drivers are capable
  809.      of checking for port availability themselves.}
  810.     
  811.     {This next bit of code is necessary to allow the default button of our
  812.      alert to be outlined.}
  813.      
  814.     FOR count := 1 TO 3 DO
  815.         ignoreResult := EventAvail(everyEvent, event);
  816.  
  817.     CatchFailures(fi, HandleErr);
  818.  
  819.     {Ignore the error returned from SysEnvirons; even if an error occurred,
  820.      the SysEnvirons glue will fill in the SysEnvRec. You can save a redundant
  821.      call to SysEnvirons by calling it after initializing AppleTalk.}
  822.      
  823.     ignoreError := SysEnvirons(kSysEnvironsVersion, gMac);
  824.     
  825.     {Make sure that the machine has at least 128K ROMs. If it doesn't, exit.}
  826.     
  827.     IF gMac.machineType < 0 THEN
  828.         Failure(0, eWrongMachine);
  829.     
  830.     {Move TrapAvailable call to after SysEnvirons so that we can tell
  831.      in TrapAvailable if a tool trap value is out of range.}
  832.      
  833.     gHasWaitNextEvent := TrapAvailable(_WaitNextEvent, ToolTrap);
  834.  
  835.     {First check the size of the application heap against a value
  836.      that you have determined is the smallest heap the application can reasonably
  837.      work in. This number should be derived by examining the size of the heap that
  838.      is actually provided by MultiFinder when the minimum size requested is used.
  839.      The derivation of the minimum size requested from MultiFinder is described
  840.      in Sample.h. The check should be made because the preferred size can end up
  841.      being set smaller than the minimum size by the user. This extra check acts to
  842.      insure that your application is starting from a solid memory foundation.}
  843.      
  844.     IF ORD(GetApplLimit) - ORD(ApplicationZone) < kMinHeap THEN
  845.         Failure(0, eSmallSize);
  846.     
  847.     {Next, make sure that enough memory is free for your application to run. It
  848.      is possible for a situation to arise where the heap may have been of required
  849.      size, but a large scrap was loaded which left too little memory. To check for
  850.      this, call PurgeSpace and compare the result with a value that you have determined
  851.      is the minimum amount of free memory your application needs at initialization.
  852.      This number can be derived several different ways. One way that is fairly
  853.      straightforward is to run the application in the minimum size configuration
  854.      as described previously. Call PurgeSpace at initialization and examine the value
  855.      returned. However, you should make sure that this result is not being modified
  856.      by the scrap's presence. You can do that by calling ZeroScrap before calling
  857.      PurgeSpace. Make sure to remove that call before shipping, though.}
  858.      
  859.     PurgeSpace(total, contig);
  860.     IF total < kMinSpace THEN
  861.         Failure(0, eNoMemory);
  862.  
  863.     {The extra benefit to waitng until after the Toolbox Managers have been initialized
  864.      before checking memory is that we can now give the user an alert to tell him what
  865.      happened. Although it is possible that the memory situation could be worsened by
  866.      displaying an alert, MultiFinder would gracefully exit the application with
  867.      an informative alert if memory became critical. Here we are acting more
  868.      in a preventative manner to avoid future disaster from low-memory problems.}
  869.  
  870.     menuBar := GetNewMBar(rMenuBar);        {read menus into menu bar}
  871.     FailNILMsg(menuBar, eNoMemory);
  872.     SetMenuBar(menuBar);                    {install menus}
  873.     DisposeHandle(menuBar);
  874.     AppendResMenu(GetMenuHandle(mApple), 'DRVR');    {add DA names to Apple menu}
  875.     DrawMenuBar;
  876.     gShape := kOval;
  877.     gUseBack := TRUE;
  878.     gUseEdit := TRUE;
  879.     gOughHandle := NIL;
  880.     
  881.     {Get the 'Moof' icon. If the environment supports Color QD,
  882.      we'll get the color icon. If Color QD is not supported,
  883.      we'll still get the color icon, but use it differently.}
  884.      
  885.     IF gMac.hasColorQD THEN BEGIN
  886.         gcicn := GetCIcon(kCMoof);
  887.         FailNILMsg(gcicn, eNoMemory);
  888.     END ELSE BEGIN
  889.         gcicn := CIconHandle(GetResource('cicn', kCMoof));
  890.         FailNILMsg(gcicn, eNoMemory);
  891.     END;
  892.     
  893.     {If Color QD is supported, we'll get an 8-bit PICT of
  894.      Gigantor. If it isn't supported, we'll get a PICT
  895.      that looks better in non-color ports/}
  896.      
  897.     IF gMac.hasColorQD THEN
  898.         gPICT := GetPicture(kGigantor)
  899.     ELSE
  900.         gPICT := GetPicture(k1bitGigantor);
  901.     FailNILMsg(gPICT, eNoMemory);
  902.     
  903.     {If Color QD is supported, we'll set up a color cursor
  904.      that will be modified later. Otherwise, nothing
  905.      happens. We'll also set up a 1-bit offscreen to
  906.      make a cursor mask.}
  907.      
  908.     IF gMac.hasColorQD THEN BEGIN
  909.         gCursor := CCrsrHandle(NewHandleClear(SIZEOF(CCrsr)));
  910.         FailNILMsg(gCursor, eNoMemory);
  911.         MoveHHi(Handle(gCursor));
  912.         HLock(Handle(gCursor));
  913.         WITH gCursor^^ DO BEGIN
  914.             crsrType := $8001;
  915.             crsrXData := NewHandle(0);
  916.         END;
  917.         HUnlock(Handle(gCursor));
  918.         colors := CTabHandle(NewHandleClear(SIZEOF(ColorTable)));
  919.         FailNILMsg(colors, eNoMemory);
  920.         SetRect(bounder, 0, 0, 16, 16);
  921.         
  922.         {For this one bit deep offscreen guy (used to make cursor masks)
  923.          we pass in a zeroed but otherwise unitialized color table. Since
  924.          the map’s ctable will only have B&W anyway, it doesn’t matter.}
  925.          
  926.         FailOSErr(NewOffscreen(bounder, 1, colors,
  927.                         NOT kMemoryPolite, buffNotNeeded,
  928.                         g1BitHandle));
  929.         DisposeHandle(Handle(colors));
  930.     END;
  931.  
  932.     DoNewWindow;                            {create a new window right away}
  933. END; {Initialize}
  934.  
  935.  
  936. {$S Main}
  937. PROCEDURE Terminate;
  938.  
  939. {Clean up the application and exits. We close all of the windows so that
  940.  they can update their documents, if any.
  941.  If we find out that a cancel has occurred, we won't exit to the
  942.  shell, but will return instead.}
  943.  
  944. VAR
  945.     aWindow    : WindowPtr;
  946.     closed    : BOOLEAN;
  947.  
  948. BEGIN
  949.     closed := TRUE;
  950.     REPEAT
  951.         aWindow := FrontWindow;                    {get the current front window}
  952.         IF aWindow <> NIL THEN
  953.             closed := DoCloseWindow(aWindow);    {close this window}
  954.     UNTIL (NOT closed) | (aWindow = NIL);        {do all windows}
  955.     IF closed THEN
  956.         ExitToShell;                            {exit if no cancellation}
  957. END; {Terminate}
  958.  
  959.  
  960. {$S Main}
  961. PROCEDURE AdjustMenus;
  962.  
  963. {Enable and disable menus based on the current state.
  964.  The user can only select enabled menu items. We set up all the menu items
  965.  before calling MenuSelect or MenuKey, since these are the only times that
  966.  a menu item can be selected. Note that MenuSelect is also the only time
  967.  the user will see menu items. This approach to deciding what enable/
  968.  disable state a menu item has the advantage of concentrating all the decision-
  969.  making in one routine, as opposed to being spread throughout the application.
  970.  Other application designs may take a different approach that may or may not be
  971.  just as valid.}
  972.  
  973. VAR
  974.     window            : WindowPtr;
  975.     menu            : MenuHandle;
  976.  
  977. BEGIN
  978.     window := FrontWindow;
  979.  
  980.     menu := GetMenuHandle(mFile);
  981.     IF IsDAWindow(window) |
  982.         IsAppWindow(window) THEN            {we can allow DAs to be closed from the menu}
  983.         EnableItem(menu, iClose)
  984.     ELSE
  985.         DisableItem(menu, iClose);
  986.  
  987.     menu := GetMenuHandle(mEdit);
  988.     IF IsDAWindow(window) THEN BEGIN        {a desk accessory might need the edit menu}
  989.         EnableItem(menu, iUndo);
  990.         EnableItem(menu, iCut);
  991.         EnableItem(menu, iCopy);
  992.         EnableItem(menu, iPaste);
  993.         EnableItem(menu, iClear);
  994.     END ELSE BEGIN                            {but we know we do not}
  995.         DisableItem(menu, iUndo);
  996.         DisableItem(menu, iCut);
  997.         DisableItem(menu, iCopy);
  998.         DisableItem(menu, iClear);
  999.         DisableItem(menu, iPaste);
  1000.     END;
  1001.  
  1002.     menu := GetMenuHandle(mSpecial);
  1003.     IF gMac.hasColorQD & IsAppWindow(window) THEN
  1004.         EnableItem(menu, iPickColor)        {color can change only if we are top}
  1005.     ELSE
  1006.         DisableItem(menu, iPickColor);
  1007. END; {AdjustMenus}
  1008.  
  1009.  
  1010. {$S Main}
  1011. PROCEDURE DoMenuCommand(menuResult: LONGINT);
  1012.  
  1013. {This is called when an item is chosen from the menu bar (after calling
  1014.  MenuSelect or MenuKey). It performs the right operation for each command.
  1015.  It is good to have both the result of MenuSelect and MenuKey go to
  1016.  one routine like this to keep everything organized.}
  1017.  
  1018. VAR
  1019.     menuID            : INTEGER;        {the resource ID of the selected menu}
  1020.     menuItem        : INTEGER;        {the item number of the selected menu}
  1021.     int                : INTEGER;
  1022.     str                : Str255;
  1023.     ignore            : BOOLEAN;
  1024.  
  1025. BEGIN
  1026.     menuID := HiWord(menuResult);    {use built-ins (for efficiency)...}
  1027.     menuItem := LoWord(menuResult);    {to get menu item number and menu number}
  1028.     CASE menuID OF
  1029.         mApple:
  1030.             CASE menuItem OF
  1031.                 iAbout:                {bring up alert for About}
  1032.                     int := Alert(rAboutAlert, NIL);
  1033.                 OTHERWISE BEGIN        {all non-About items in this menu are DAs}
  1034.                     GetMenuItemText(GetMenuHandle(mApple), menuItem, str);
  1035.                     int := OpenDeskAcc(str);
  1036.                 END;
  1037.             END;
  1038.         mFile:
  1039.             CASE menuItem OF
  1040.                 iNew:
  1041.                     DoNewWindow;
  1042.                 iClose:
  1043.                     ignore := DoCloseWindow(FrontWindow); {we don't care if cancelled}
  1044.                 iQuit:
  1045.                     Terminate;
  1046.             END;
  1047.         mEdit:                        {call SystemEdit for DA editing & MultiFinder}
  1048.             ignore := SystemEdit(menuItem-1);    {since we don't do any editing}
  1049.         mShape: 
  1050.             IF gShape <> Shapes(menuItem - 1) THEN BEGIN
  1051.                 CheckItem(GetMenuHandle(mShape), ORD(gShape) + 1, FALSE);
  1052.                 gShape := Shapes(menuItem - 1);        {the shape is the item}
  1053.                 CheckItem(GetMenuHandle(mShape), ORD(gShape) + 1, TRUE);
  1054.                 IF gMac.hasColorQD & IsAppWindow(FrontWindow) THEN
  1055.                     SetObjCursor(FrontWindow);
  1056.             END;
  1057.         mSpecial:
  1058.             CASE menuItem OF
  1059.                 iUseBack: BEGIN
  1060.                     gUseBack := NOT gUseBack;
  1061.                     CheckItem(GetMenuHandle(mSpecial), iUseBack, gUseBack);
  1062.                 END;
  1063.                 iUseEdit: BEGIN
  1064.                     gUseEdit := NOT gUseEdit;
  1065.                     CheckItem(GetMenuHandle(mSpecial), iUseEdit, gUseEdit);
  1066.                 END;
  1067.                 iPickColor:
  1068.                     ChangeColor(FrontWindow);
  1069.             END;
  1070.     END;
  1071.     HiliteMenu(0);                    {unhighlight what MenuSelect (or MenuKey) hilited}
  1072. END; {DoMenuCommand}
  1073.  
  1074.  
  1075. {$S Main}
  1076. PROCEDURE GoThroughShapes (PROCEDURE WhatToDo(shape: Shapes); window: WindowPtr);
  1077.  
  1078. {Go through the list of shapes for window and
  1079.  call WhatToDo, passing the shape we are on
  1080.  each time.}
  1081.  
  1082. VAR
  1083.     theShape    : Shapes;
  1084.     
  1085. BEGIN
  1086.     WITH OffscreenPeek(window)^ DO
  1087.         IF ORD(fFirst) <> kNotDrawn THEN BEGIN
  1088.             theShape := fFirst;
  1089.             REPEAT
  1090.                 WhatToDo(theShape);
  1091.                 theShape := fShapes[theShape].next;
  1092.             UNTIL ORD(theShape) = kLastOne;
  1093.         END;
  1094. END; {GoThroughShapes}
  1095.  
  1096.  
  1097. {$S Main}
  1098. PROCEDURE DrawAllShapes (window: WindowPtr; doEdit: BOOLEAN);
  1099.  
  1100. {Draw either the currently edited shape or all the shapes
  1101.  in the window's list. Called by DrawWindow.}
  1102.  
  1103. VAR
  1104.     area    : Rect;
  1105.     
  1106.     PROCEDURE AndDrawThem (shape: Shapes);
  1107.     
  1108.     VAR
  1109.         r    : Rect;
  1110.         
  1111.     BEGIN
  1112.         WITH OffscreenPeek(window)^ DO
  1113.             IF shape <> fEdit THEN BEGIN
  1114.                 IF SectRect(OffscreenPeek(window)^.fShapes[shape].extent, area, r)
  1115.                     THEN BEGIN
  1116.                     IF gMac.hasColorQD THEN
  1117.                         PmForeColor(ORD(shape) + 2);
  1118.                     DrawShape(shape, OffscreenPeek(window)^.fShapes[shape].extent);
  1119.                 END;
  1120.             END;
  1121.     END;
  1122.  
  1123. BEGIN
  1124.     SetPort(window);
  1125.     IF doEdit THEN BEGIN
  1126.         WITH OffscreenPeek(window)^ DO                {draw edit shape}
  1127.             IF ORD(fEdit) <> kNotDrawn THEN BEGIN
  1128.                 IF gMac.hasColorQD THEN
  1129.                     PmForeColor(ORD(fEdit) + 2);
  1130.                 DrawShape(fEdit, OffscreenPeek(window)^.fShapes[fEdit].extent);
  1131.             END;
  1132.     END ELSE BEGIN
  1133.         area := window^.visRgn^^.rgnBBox;
  1134.         GoThroughShapes(AndDrawThem, window);
  1135.     END;
  1136. END; {DrawAllShapes}
  1137.  
  1138.  
  1139. {$S Main}
  1140. PROCEDURE DrawWindow(window: WindowPtr);
  1141.  
  1142. {The core application window updating routine. Understands about Offscreen
  1143.  setup, (in this case, two nested offscreen buffers), and what needs to
  1144.  be drawn, in this case, a whole bunch of shapes. Called from two routines,
  1145.  DoUpdate and DoContentClick. The way it works is first, by calling
  1146.  BeginUpdateOffscreen on fEditHandle, the drawing is redirected to
  1147.  the 'edit' offscreen pixmap. Next, if any drawing needs to be done
  1148.  in the 'background' pixmap, then by calling BeginOffscreenDrawing on
  1149.  fBackHandle, drawing is further redirected. All the shapes that exist
  1150.  but are not the one being edited (i.e., the background) are drawn here
  1151.  and the EndOffscreenDrawing causes the redirecting to cease. Then the
  1152.  pixmap is copybitsed into the next outer layer of drawing, whether that
  1153.  is the 'edit' offscreen pixmap or the window itself. There, the shape
  1154.  being edited is drawn. Finally, EndUpdateOffscreen is called to cease
  1155.  that layer of redirection and copybits the 'edit' offscreen to the window.
  1156.  The way this is designed, it all still works if either or both of the
  1157.  offscreen pixmaps is missing.}
  1158.  
  1159. VAR
  1160.     globalRect    : Rect;
  1161.     drawNeeded    : BOOLEAN;
  1162.     backMap        : BitMapPtr;
  1163.     
  1164. BEGIN
  1165.     GetGlobalRect(window, globalRect);
  1166.     WITH OffscreenPeek(window)^ DO BEGIN
  1167.         IF CheckBoundsOffscreen(fEditHandle, globalRect, drawNeeded) <> noErr THEN {do nada};
  1168.         SetPort(window);
  1169.         BeginUpdateOffscreen(fEditHandle, window);    {this sets up the visRgn}
  1170.         
  1171.         IF CheckBoundsOffscreen(fBackHandle, globalRect, drawNeeded) <> noErr THEN 
  1172.                                                     {do nada};
  1173.         IF drawNeeded THEN BEGIN                    {draw if updating needs to be done}
  1174.             BeginOffscreenDrawing(fBackHandle, window);
  1175.             EraseRect(window^.portRect);            {clear out any garbage that might}
  1176.             DrawAllShapes(window, FALSE);            {be left behind and draw the}
  1177.             EndOffscreenDrawing(fBackHandle);        {'background'}
  1178.         END;
  1179.         backMap := GetMap(fBackHandle);
  1180.         IF backMap <> NIL THEN BEGIN
  1181.             ForeColor(blackColor);
  1182.             BackColor(whiteColor);                    {so funny colorization doesn't happen}
  1183.             WITH window^ DO BEGIN
  1184.                 CopyBits(backMap^, portBits, portRect, portRect, srcCopy, NIL);
  1185.                 ValidRectOffscreen(fBackHandle, NIL, portRect);
  1186.             END;
  1187.         END;
  1188.         DrawAllShapes(window, TRUE);                {only draw the edited shape}
  1189.         
  1190.         EndUpdateOffscreen(fEditHandle, window);
  1191.         CheckTitle(window, TRUE);                    {buffers may have changed}
  1192.     END;
  1193. END; {DrawWindow}
  1194.  
  1195.  
  1196. {$S Main}
  1197. PROCEDURE DoContentClick (window: WindowPtr; event: EventRecord);
  1198.  
  1199. {This is called when a mouse-down event occurs in the content of a window.
  1200.  Other applications might want to call FindControl, TEClick, etc., to
  1201.  further process the click. In Offsample, a user click in the content
  1202.  region means a shape is to be added or changed.}
  1203.  
  1204. VAR
  1205.     oldRect, newRect        : Rect;
  1206.     anchorPt, oldPt, nextPt    : Point;
  1207.     lastShape                : Shapes;
  1208.     first                    : BOOLEAN;
  1209.     
  1210.     PROCEDURE AndReorderThem (shape: Shapes);
  1211.     
  1212.     {Remove the edited shape from the linked list of shapes.}
  1213.     
  1214.     BEGIN
  1215.         WITH OffscreenPeek(window)^ DO
  1216.             IF shape <> gShape THEN
  1217.                 lastShape := shape
  1218.             ELSE
  1219.                 IF fFirst = shape THEN
  1220.                     fFirst := fShapes[shape].next
  1221.                 ELSE
  1222.                     fShapes[lastShape].next := fShapes[shape].next;
  1223.     END; {AndReorderThem}
  1224.  
  1225. BEGIN
  1226.     IF IsAppWindow(window) THEN
  1227.         WITH OffscreenPeek(window)^ DO BEGIN
  1228.             anchorPt := event.where;
  1229.             GlobalToLocal(anchorPt);
  1230.             oldPt := anchorPt;
  1231.             
  1232.             {If the shape being edited existed previously, we need
  1233.              to invalidate its old position so that it gets
  1234.              'erased'.}
  1235.              
  1236.             IF ORD(fShapes[gShape].next) <> kNotDrawn THEN BEGIN
  1237.                 oldRect := GetInvalExtent(window, gShape);
  1238.                 InvalRectOffscreen(fBackHandle, NIL, oldRect);
  1239.                 InvalRectOffscreen(fEditHandle, window, oldRect);
  1240.             END;
  1241.             fEdit := gShape;                            {flag this shape as edited}
  1242.             lastShape := Shapes(kLastOne);
  1243.             GoThroughShapes(AndReorderThem, window);
  1244.             IF ORD(lastShape) <> kLastOne THEN
  1245.                 fShapes[lastShape].next := gShape        {make edited shape last}
  1246.             ELSE
  1247.                 fFirst := gShape;                        {or if only shape, first}
  1248.             fShapes[gShape].next := Shapes(kLastOne);
  1249.             Pt2Rect(anchorPt, anchorPt, oldRect);
  1250.             first := TRUE;                                {indicate first time though loop}
  1251.             WHILE WaitMouseUp DO BEGIN                    {while the mouse is down…}
  1252.                 GetMouse(nextPt);
  1253.                 IF first | (NOT EqualPt(oldPt, nextPt)) THEN BEGIN
  1254.                     first := FALSE;                        {no longer first time through loop}
  1255.                     oldPt := nextPt;
  1256.                     CASE gShape OF
  1257.                         kOval,                            {build a rectangle for these}
  1258.                         kRegion,                        {from the anchor point and}
  1259.                         kRRect,                            {the current point}
  1260.                         kPoly,
  1261.                         kRect:
  1262.                             Pt2Rect(anchorPt, nextPt, newRect);
  1263.                         kICON:                            {rect from current position}
  1264.                             WITH nextPt, newRect DO BEGIN
  1265.                                 top := v;
  1266.                                 left := h;
  1267.                                 bottom := top + 32;
  1268.                                 right := left + 32;
  1269.                             END;
  1270.                         kPICT:                            {rect from current position}
  1271.                             WITH nextPt, newRect DO BEGIN
  1272.                                 newRect := gPICT^^.picFrame;
  1273.                                 OffsetRect(newRect, -left, -top);
  1274.                                 OffsetRect(newRect, h, v);
  1275.                             END;
  1276.                     END;
  1277.                     fShapes[gShape].extent := newRect;
  1278.                     UnionRect(newRect, oldRect, oldRect);
  1279.                     
  1280.                     {In the case of the 'stretchable' shapes whose extents are
  1281.                      built from the anchor point and the current point, doing
  1282.                      a UnionRect is pretty close to being as efficient as doing
  1283.                      a UnionRgn with two regions that are shaped like the old
  1284.                      and new extents. However, a case can be made for using
  1285.                      regions for the icon and the picture since they move around
  1286.                      instead of 'stretching'. The effect of extra, unnecessary
  1287.                      invalidation is, of course, most noticeable when there is
  1288.                      no edit offscreen and the icon/picture is moved around
  1289.                      rapidly. Changing the code to use regions is LEFT AS AN
  1290.                      EXERCISE FOR THE READER, Ha-Ha-Ha.}
  1291.                      
  1292.                     InvalRectOffscreen(fEditHandle, window, oldRect);
  1293.                     DrawWindow(window);
  1294.                     oldRect := GetInvalExtent(window, gShape);
  1295.                 END;
  1296.             END;
  1297.             fEdit := Shapes(kNotDrawn);
  1298.             oldRect := GetInvalExtent(window, gShape);
  1299.             InvalRectOffscreen(fBackHandle, NIL, oldRect);
  1300.         END;
  1301. END; {DoContentClick}
  1302.  
  1303.  
  1304. {$S Main}
  1305. PROCEDURE DoUpdate(window: WindowPtr);
  1306.  
  1307. {This is called when an update event is received for a window.
  1308.  It calls DrawWindow to draw the contents of an application window.}
  1309.  
  1310. BEGIN
  1311.     IF IsAppWindow(window) THEN
  1312.         DrawWindow(window);
  1313. END; {DoUpdate}
  1314.  
  1315.  
  1316. {$S Main}
  1317. PROCEDURE DoActivate(window: WindowPtr; becomingActive: BOOLEAN);
  1318.  
  1319. {This is called when a window is activated or deactivated.}
  1320.  
  1321. BEGIN
  1322.     IF IsAppWindow(window) THEN
  1323.         IF gMac.hasColorQD & becomingActive THEN
  1324.             SetObjCursor(window);
  1325. END; {DoActivate}
  1326.  
  1327.  
  1328. {$S Main}
  1329. PROCEDURE AdjustCursor(region: RgnHandle);
  1330.  
  1331. {Change the cursor's shape, depending on its position. This also calculates the region
  1332.  where the current cursor resides (for WaitNextEvent). If the mouse is ever outside of
  1333.  that region, an event is generated, causing this routine to be called. This
  1334.  allows us to change the region to the region the mouse is currently in. If
  1335.  there is more to the event than just “the mouse moved”, we get called before the
  1336.  event is processed to make sure the cursor is the right one. In any (ahem) event,
  1337.  this is called again before we fall back into WNE.}
  1338.  
  1339. VAR
  1340.     window                : WindowPtr;
  1341.     arrowRgn            : RgnHandle;
  1342.     shapeRgn            : RgnHandle;
  1343.     globalPortRect        : Rect;
  1344.     mouse                : Point;
  1345.  
  1346. BEGIN
  1347.     window := FrontWindow;
  1348.     {we only adjust the cursor when we are in front}
  1349.     IF (NOT gInBackground) AND (NOT IsDAWindow(window)) THEN BEGIN
  1350.         GetMouse(mouse);
  1351.         LocalToGlobal(mouse);
  1352.         
  1353.         {calculate regions for different cursor shapes}
  1354.         arrowRgn := NewRgn;
  1355.         shapeRgn := NewRgn;
  1356.  
  1357.         {start with a big, big rectangular region}
  1358.         SetRectRgn(arrowRgn, kExtremeNeg, kExtremeNeg,
  1359.                             kExtremePos, kExtremePos);
  1360.  
  1361.         {calculate shapeRgn}
  1362.         IF IsAppWindow(window) THEN BEGIN
  1363.             SetPort(window);            {make a global version of the portRect}
  1364.             IF gMac.hasColorQD THEN
  1365.                 WITH CGrafPtr(window)^ DO
  1366.                     SetOrigin(-portPixMap^^.bounds.left, -portPixMap^^.bounds.top)
  1367.             ELSE
  1368.                 WITH window^.portBits.bounds DO
  1369.                     SetOrigin(-left, -top);
  1370.             globalPortRect := window^.portRect;
  1371.             RectRgn(shapeRgn, globalPortRect);
  1372.             SectRgn(shapeRgn, window^.visRgn, shapeRgn);
  1373.             SetOrigin(0, 0);
  1374.         END;
  1375.  
  1376.         {subtract other regions from arrowRgn}
  1377.         DiffRgn(arrowRgn, shapeRgn, arrowRgn);
  1378.  
  1379.         {change the cursor and the region parameter}
  1380.         IF PtInRgn(mouse, shapeRgn) THEN BEGIN
  1381.             IF gMac.hasColorQD THEN
  1382.                 SetCCursor(gCursor)
  1383.             ELSE
  1384.                 SetCursor(GetCursor(crossCursor)^^);
  1385.             CopyRgn(shapeRgn, region);
  1386.         END ELSE BEGIN
  1387.             SetCursor(qd.arrow);
  1388.             CopyRgn(arrowRgn, region);
  1389.         END;
  1390.  
  1391.         {get rid of our local regions}
  1392.         DisposeRgn(arrowRgn);
  1393.         DisposeRgn(shapeRgn);
  1394.     END;
  1395. END; {AdjustCursor}
  1396.  
  1397.  
  1398. {$S Main}
  1399. PROCEDURE DoEvent(event: EventRecord);
  1400.  
  1401. {Do the right thing for an event. Determine what kind of event it is, and call
  1402.  the appropriate routines.}
  1403.  
  1404. VAR
  1405.     part, err    : INTEGER;
  1406.     window        : WindowPtr;
  1407.     ignore        : BOOLEAN;
  1408.     key            : CHAR;
  1409.     aPoint        : Point;
  1410.     fi            : FailInfo;
  1411.     
  1412.     PROCEDURE HandleErr(error: INTEGER; message: LongInt);
  1413.     BEGIN
  1414.         IF error > 0 THEN
  1415.             AlertUser(0, error)
  1416.         ELSE
  1417.             AlertUser(error, message);
  1418.         EXIT(DoEvent);
  1419.     END; {HandleErr}
  1420.  
  1421. BEGIN
  1422.     CatchFailures(fi, HandleErr);
  1423.     CASE event.what OF
  1424.         mouseDown: BEGIN
  1425.             part := FindWindow(event.where, window);
  1426.             CASE part OF
  1427.                 inMenuBar: BEGIN            {process the menu command}
  1428.                     AdjustMenus;
  1429.                     DoMenuCommand(MenuSelect(event.where));
  1430.                 END;
  1431.                 inSysWindow:                {let the system handle the mouseDown}
  1432.                     SystemClick(event, window);
  1433.                 inContent:
  1434.                     IF window <> FrontWindow THEN BEGIN
  1435.                         SelectWindow(window);
  1436.                         {DoEvent(event);}    {use this line for "do first click"}
  1437.                     END ELSE
  1438.                         DoContentClick(window, event);
  1439.                 inDrag:                        {pass screenBits.bounds to get all gDevices}
  1440.                     DragWindow(window, event.where, qd.screenBits.bounds);
  1441.                 inGrow:;
  1442.                 inZoomIn, inZoomOut:;
  1443.                 inGoAway:
  1444.                     IF TrackGoAway(window, event.where) THEN
  1445.                         ignore := DoCloseWindow(window);
  1446.             END;
  1447.         END;
  1448.         keyDown, autoKey: BEGIN                {check for menukey equivalents}
  1449.             key := CHR(BAnd(event.message, charCodeMask));
  1450.             IF BAnd(event.modifiers, cmdKey) <> 0 THEN    {Command key down}
  1451.                 IF event.what = keyDown THEN BEGIN
  1452.                     AdjustMenus;            {enable/disable/check menu items properly}
  1453.                     DoMenuCommand(MenuKey(key));
  1454.                 END;
  1455.         END;                                {call DoActivate with the window and...}
  1456.         activateEvt:                        {TRUE for activate, FALSE for deactivate}
  1457.             DoActivate(WindowPtr(event.message), BAnd(event.modifiers, activeFlag) <> 0);
  1458.         updateEvt:                          {call DoUpdate with the window to update}
  1459.             DoUpdate(WindowPtr(event.message));
  1460.         diskEvt:
  1461.             IF HiWord(event.message) <> noErr THEN BEGIN
  1462.                 SetPt(aPoint, kDILeft, kDITop);
  1463.                 err := DIBadMount(aPoint, event.message);
  1464.             END;
  1465.         kOSEvent:
  1466.             CASE BAnd(BRotL(event.message, 8), $FF) OF    {high byte of message}
  1467.                 kSuspendResumeMessage: BEGIN
  1468.                     gInBackground := BAnd(event.message, kResumeMask) = 0;
  1469.                     DoActivate(FrontWindow, NOT gInBackground);
  1470.                 END;
  1471.             END;
  1472.     END;
  1473.     Success(fi);
  1474. END; {DoEvent}
  1475.  
  1476.  
  1477. {$S Main}
  1478. PROCEDURE EventLoop;
  1479.  
  1480. {Get events forever, and handle them by calling DoEvent.
  1481.  Get the events by calling WaitNextEvent, if it's available, otherwise
  1482.  by calling GetNextEvent. Also call AdjustCursor each time through the loop.}
  1483.  
  1484. VAR
  1485.     cursorRgn    : RgnHandle;
  1486.     gotEvent    : BOOLEAN;
  1487.     event        : EventRecord;
  1488.  
  1489. BEGIN
  1490.     cursorRgn := NewRgn;            {we’ll pass WNE an empty region the 1st time thru}
  1491.     REPEAT
  1492.         IF gHasWaitNextEvent THEN {put us 'asleep' forever under MultiFinder}
  1493.             gotEvent := WaitNextEvent(everyEvent, event, MAXLONGINT, cursorRgn)
  1494.         ELSE BEGIN
  1495.             SystemTask;                {must be called if using GetNextEvent}
  1496.             gotEvent := GetNextEvent(everyEvent, event);
  1497.         END;
  1498.         IF gotEvent THEN BEGIN
  1499.             AdjustCursor(cursorRgn); {make sure we have the right cursor}
  1500.             DoEvent(event);
  1501.         END;
  1502.         AdjustCursor(cursorRgn);
  1503.     UNTIL FALSE;                    {loop forever; we quit through an ExitToShell}
  1504. END; {EventLoop}
  1505.  
  1506.  
  1507. PROCEDURE _DataInit; EXTERNAL;
  1508.  
  1509. {This routine is part of the MPW runtime library. This external
  1510.  reference to it is done so that we can unload its segment, %A5Init.}
  1511.  
  1512. {$S Main}
  1513. BEGIN
  1514.     UnloadSeg(@_DataInit);    {note that _DataInit must not be in Main!}
  1515.     
  1516.     MoreMasters;
  1517.     MoreMasters;
  1518.     MoreMasters;            {prepare for handles used by Offscreen}
  1519.     
  1520.     {If you have stack requirements that differ from the default,
  1521.      then you could use SetApplLimit to increase StackSpace at 
  1522.      this point, before calling MaxApplZone.}
  1523.      
  1524.     MaxApplZone;            {expand the heap so code segments load at the top}
  1525.  
  1526.     InitSignals;
  1527.     Initialize;                {initialize the program}
  1528.     UnloadSeg(@Initialize);    {note that Initialize must not be in Main!}
  1529.  
  1530.     EventLoop;                {call the main event loop}
  1531. END.
  1532.